#include "sysinfo.h"
#include <syslog.h>

#include <QFile>
#include <QTextStream>
#include <QApplication>
#include "utils.h"

SysInfo::SysInfo(QWidget *parent) :
    QFrame(parent), old_cpu_inuse(0), old_cpu_total(0)
{
    this->setMinimumSize(0, 0);
    this->hide();
    this->setFocusPolicy(Qt::WheelFocus);

    m_cpu_label.setText("CPU");
    m_cpu_label.setMaximumHeight(20);
    m_cpu_label.setAttribute(Qt::WA_TransparentForMouseEvents);
    m_cpu_bar.setOrientation(Qt::Vertical);
    m_cpu_bar.setAttribute( Qt::WA_TransparentForMouseEvents);
    m_cpu_layout.addWidget(&m_cpu_label);
    m_cpu_layout.addWidget(&m_cpu_bar);
    m_cpu_layout.setMargin(0);

    m_mem_label.setText("MEM");
    m_mem_label.setMaximumHeight(20);
    m_mem_bar.setOrientation(Qt::Vertical);
    m_mem_layout.addWidget(&m_mem_label);
    m_mem_layout.addWidget(&m_mem_bar);
    m_mem_layout.setMargin(0);

    m_main_layout.addLayout(&m_cpu_layout);
    m_main_layout.addLayout(&m_mem_layout);
    m_main_layout.setMargin(5);

    this->setLayout(&m_main_layout);
}

double SysInfo::read_cpu()
{
    QFile proc_file("/proc/stat");
    if(! proc_file.open(QFile::ReadOnly | QFile::Text)) {
        syslog(LOG_ERR, "Read proc file Failure, Please Check it !!!\n");
        return 0;
    }
    QTextStream stream(&proc_file);
    QApplication::setOverrideCursor(Qt::WaitCursor);

    /* Start Conllect Data */
    QString line = stream.readLine();

    QApplication::restoreOverrideCursor();
    proc_file.close();

    int cpu_user, cpu_nice, cpu_system, cpu_idle;
    char text[26];
    if(0 == sscanf(line.toLocal8Bit().data(), "%s %d %d %d %d", text, &cpu_user, &cpu_nice, &cpu_system, &cpu_idle)) {
        syslog(LOG_ERR, "Sscanf format failure\n");
        return 0;
    }
    int cpu_total = cpu_user + cpu_nice + cpu_system + cpu_idle;
    int cpu_inuse = cpu_user + cpu_nice + cpu_system;

    double value = (double)(cpu_inuse - old_cpu_inuse) / (cpu_total - old_cpu_total);
    old_cpu_total = cpu_total;
    old_cpu_inuse = cpu_inuse;

    return value;
}

double SysInfo::read_mem()
{
    QFile proc_file("/proc/meminfo");
    if(! proc_file.open(QFile::ReadOnly | QFile::Text)) {
        syslog(LOG_ERR, "Read proc file Failure, Please Check it !!!\n");
        return 0;
    }
    QTextStream stream(&proc_file);
    QApplication::setOverrideCursor(Qt::WaitCursor);

    /* Start Collect Data */
    int mem_total = stream.readLine().split(" ", QString::SkipEmptyParts).at(1).trimmed().toInt();
    int mem_use = mem_total - stream.readLine().split(" ", QString::SkipEmptyParts).at(1).trimmed().toInt();

    QApplication::restoreOverrideCursor();
    proc_file.close();

    return (double)mem_use / mem_total;
}

void SysInfo::update_info_gui(double &cpu_percent, double &mem_percent)
{
    m_cpu_bar.setValue((int)(cpu_percent * 100));
    m_mem_bar.setValue((int)(mem_percent * 100));
}

void SysInfo::focusOutEvent(QFocusEvent *)
{
    this->setVisible(false);

    emit hide_change_mainwindow_size();
}
